import router from "../router";
import {getToken} from "./loginFunctions";
import {selectClassState} from "./index";
import {computed} from "vue";
import store from "../store";
let Tokencheckrouter = () => {
    //路由拦截
    router.beforeEach((to, from, next) => {
        //判断进入路由之前，查看元信息是否需要登入
        if (to.meta.requiresAuth && !to.meta.requiresAddPermission) {
            if (getToken()) {
                next();
            }else {
                next({
                    path: '/',
                    // query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }
        }else if(to.meta.requiresAuth && to.meta.requiresAddPermission){
            store.dispatch("getAddPermission").then(()=>{
                if(store.state.addPermission){
                    next();
                }
            })

        }else if(to.path === "/" && getToken()){
            next({
                path: '/home',
            })
        }
        else {
            next();
        }
    })
}

export default Tokencheckrouter
